﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;

namespace WinnerCMS.Common
{
    public class XmlBase
    {

        protected string StrXmlFile;
        protected XmlDocument XmlDoc = new XmlDocument();

        public XmlBase(string xmlFile)
        {
            if (!File.Exists(xmlFile))
            {
                CreatXmlFile(xmlFile);
            }
            this.XmlDoc.Load(xmlFile);


            this.StrXmlFile = xmlFile;
        }
        protected void CreatXmlFile(string file)
        {
            XmlTextWriter writer = new XmlTextWriter(file, Encoding.UTF8);

            // start writing! 
            writer.WriteStartDocument();
            writer.WriteStartElement("Root");
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
        }
        /// <summary> 
        /// 查找数据。返回一个DataSet 多条 
        /// </summary> 
        /// <param name="xmlPathNode">结点路径 </param> 
        /// <returns> </returns> 
        public DataSet GetData(string xmlPathNode)
        {
            //查找数据。返回一个DataSet 
            DataSet ds = new DataSet();
            //=========多个================= 
            foreach (XmlNode xmlnode in this.XmlDoc.SelectNodes(xmlPathNode))
            {
                StringReader read = new StringReader(xmlnode.OuterXml);
                ds.ReadXml(read);
            }
            //============================== 
            return ds;
        }
        /// <summary> 
        /// 查找数据。返回一个DataSet 单条 
        /// </summary> 
        /// <param name="xmlPathNode"> </param> 
        /// <returns> </returns> 
        public DataSet GetDataSingle(string xmlPathNode)
        {
            //查找数据。返回一个DataSet 
            DataSet ds = new DataSet();
            //==========单个================ 
            StringReader read = new StringReader(this.XmlDoc.SelectSingleNode(xmlPathNode).OuterXml);
            ds.ReadXml(read);
            //============================== 
            return ds;
        }
        /// <summary> 
        /// 更新节点内容 
        /// </summary> 
        /// <param name="xmlPathNode"> </param> 
        /// <param name="content"> </param> 
        public void Replace(string xmlPathNode, string content)
        {
            //更新节点内容。 
            this.XmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;
        }
        /// <summary> 
        /// 删除一个节点 
        /// </summary> 
        /// <param name="node"> </param> 
        public void Delete(string node)
        {
            //删除一个节点。 
            string mainNode = node.Substring(0, node.LastIndexOf("/", StringComparison.Ordinal));
            this.XmlDoc.SelectSingleNode(mainNode).RemoveChild(this.XmlDoc.SelectSingleNode(node));
        }
        /// <summary> 
        /// 插入一个节点和此节点的一子节点 
        /// </summary> 
        /// <param name="mainNode"> </param> 
        /// <param name="childNode"> </param> 
        /// <param name="element"> </param> 
        /// <param name="content"> </param> 
        public void InsertNode(string mainNode, string childNode, string element, string content)
        {
            //插入一个节点和此节点的一子节点。 
            XmlNode objRootNode = this.XmlDoc.SelectSingleNode(mainNode);
            XmlElement objChildNode = this.XmlDoc.CreateElement(childNode);
            if (objRootNode == null)
            {
                return;
            } 
            objRootNode.AppendChild(objChildNode);
            XmlElement objElement = this.XmlDoc.CreateElement(element);
            objElement.InnerText = content;
            objChildNode.AppendChild(objElement);
        }
        /// <summary> 
        /// 插入一个节点,带一属性 
        /// </summary> 
        /// <param name="mainNode"> </param> 
        /// <param name="element"> </param> 
        /// <param name="attrib"> </param> 
        /// <param name="attribContent"> </param> 
        /// <param name="content"> </param> 
        public void InsertElement(string mainNode, string element, string attrib, string attribContent, string content)
        {
            //插入一个节点,带一属性。 
            XmlNode objNode = this.XmlDoc.SelectSingleNode(mainNode);
            XmlElement objElement = this.XmlDoc.CreateElement(element);
            objElement.SetAttribute(attrib, attribContent);
            objElement.InnerText = content;
            objNode.AppendChild(objElement);
        }
        /// <summary> 
        /// 插入一个节点,不带属性 
        /// </summary> 
        /// <param name="mainNode"> </param> 
        /// <param name="element"> </param> 
        /// <param name="content"> </param> 
        public void InsertElement(string mainNode, string element, string content)
        {
            XmlNode objNode = this.XmlDoc.SelectSingleNode(mainNode);
            XmlElement objElement = this.XmlDoc.CreateElement(element);
            objElement.InnerText = content;
            objNode.AppendChild(objElement);
        }
        /// <summary> 
        /// 保存文件 
        /// </summary> 
        public void Save()
        {
            //保存文件。 
            try
            {
                this.XmlDoc.Save(this.StrXmlFile);
            }
            catch
            {
            }
            this.XmlDoc = null;
        }

    }
}